from langchain_core.messages import HumanMessage
from langchain_openai import ChatOpenAI

import langchain
from langchain_core.tools import tool

from config.model_config import get_chat_openai_xin, get_chat_openai_zhipu

print(langchain.__version__)

@tool
def search(query: str) -> list[str]:
    """当需要获取有关四大名著的相关信息时，使用此工具搜索知识库并返回相关信息"""
    docs = ['西游记', "三国演义"]
    return docs
# 定义工具结构（JSON Schema格式）
# 工具类别
tools = [search]

config = {
        "api_base_url": "https://open.bigmodel.cn/api/paas/v4",
        "api_key": "e440104944fe4defabccd2620e5dff6c.xj8mlkvxPdr0HYff",
    }
model_name = "glm-4-flash-250414"
model = ChatOpenAI(
    openai_api_key=config.get("api_key", "EMPTY"),
    openai_api_base=config.get("api_base_url"),
    model_name=model_name,
    # openai_proxy=config.get("openai_proxy"),
    # default_headers={"X-Server-Param": config.get("secret")},
)

model_with_tools = model.bind_tools(tools)

message = HumanMessage(
    content="请提供有关四大名著的信息",
    tools=tools  # 显式传递工具信息
)

# 调用工具
response = model_with_tools.invoke([message])

print(response)

# print(response.tool_calls[0]['name'])
# 失败
